# coding: UTF-8
# Filename : search.py
# Company : imedia
# Author : hui
# -*-coding:utf-8 -*-

from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl

def result(request):
    httputils.footprint(request)
    
    cus_id = httputils.post(request, 'cus_id')
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    stype = httputils.post(request, 'type')
    keyword = httputils.post(request, 'keyword')
    
    ratio = httputils.post(request, 'ratio')
    
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(cus_id): cus_id = const.cus_id
    if not utils.is_num(stype): stype = 0

    if validate(request):
        return validate(request)
    
    output = xml_tpl.imedia_start 
    output += make_nodes(int(stype), keyword, cus_id, size, pagenum, ratio)
    output += xml_tpl.imedia_end
    return response(output)

#生成多行记录xml节点
def make_nodes(stype, keyword, cus_id, size, pagenum, ratio):

    #商品
    part1 = '''
    select p.id, p.name, c.class_name, p.images, p.store_in_app, p.description, p.list_thumb, p.summary
    from chain_product as p left join p_class as c on p.category_id = c.id
    where p.active=1 and p.cus_id = %s and p.name like '%%'''
    part2 = '''%%' limit %s, %s'''

    #分店
    if stype == 1:
        part1 = '''
        select id, name, address, tel, longitude, latitude, description, store_in_app, images, summary
        from chain_part where active=1 and cus_id = %s and name like '%%'''
    #活动
    elif stype == 2:
        part1 = '''
        select a.id, a.name, c.id, c.class_name, a.start_time, a.end_time, a.store_in_app, a.description, a.images, a.summary
        from chain_activity as a left join p_class as c on a.category_id = c.id
        where a.active = 1 and a.cus_id = %s and a.name like '%%'''
    sql = part1 + mysql.escape(keyword) + part2
    
    start_row = utils.get_star_row(size, pagenum)
    param = [int(cus_id),start_row, int(size)]
    result = mysql.doSearch(sql, param)
    return node_var_fill(stype, result, ratio)

#节点变量填充    
def node_var_fill(stype, result, ratio):
    body_str = ''
    for obj in result:
        if stype == 0:
            body_str += make_product_node(obj, ratio, True)
        elif stype == 1:
            body_str += make_part_node(obj, ratio, True)
        elif stype == 2:
            body_str += make_activity_node(obj, ratio, True)
    return body_str

#商品节点
def make_product_node(obj, ratio, is_thumb):
    product_node = xml_tpl.product_node
    product_node = product_node.replace('{id}', str(obj[0]))
    product_node = product_node.replace('{name}', str(obj[1]))
    product_node = product_node.replace('{category}', str(obj[2]))
    product_node = product_node.replace('{image}', image_path(obj[3], ratio, is_thumb))
    product_node = product_node.replace('{storeinapp}', str(obj[4]))
    product_node = product_node.replace('{description}', str(obj[5]))
    product_node = product_node.replace('{thumb}', image_path(obj[6], ratio, is_thumb))
    product_node = product_node.replace('{summary}', str(obj[7]))
    return product_node

#分店节点
def make_part_node(obj, ratio, is_thumb):
    part_node = xml_tpl.part_node
    part_node = part_node.replace('{id}', str(obj[0]))
    part_node = part_node.replace('{name}', str(obj[1]))
    part_node = part_node.replace('{address}', str(obj[2]))
    part_node = part_node.replace('{tel}', str(obj[3]))
    part_node = part_node.replace('{longitude}', str(obj[4]))
    part_node = part_node.replace('{latitude}', str(obj[5]))
    part_node = part_node.replace('{description}', str(obj[6]))
    part_node = part_node.replace('{storeinapp}', str(obj[7]))
    part_node = part_node.replace('{image}', image_path(obj[8], ratio, is_thumb))
    part_node = part_node.replace('{summary}', str(obj[9]))
    return part_node

#活动节点
def make_activity_node(obj, ratio, is_thumb):
    activity_node = xml_tpl.activity_node
    activity_node = activity_node.replace('{id}', str(obj[0]))
    activity_node = activity_node.replace('{name}', str(obj[1]))
    activity_node = activity_node.replace('{category_id}', str(obj[2]))
    activity_node = activity_node.replace('{category_name}', str(obj[3]))
    activity_node = activity_node.replace('{from}', str(obj[4]))
    activity_node = activity_node.replace('{to}', str(obj[5]))
    activity_node = activity_node.replace('{storeinapp}', str(obj[6]))
    activity_node = activity_node.replace('{description}', str(obj[7]))
    activity_node = activity_node.replace('{image}', image_path(obj[8], ratio, is_thumb))
    activity_node = activity_node.replace('{summary}', str(obj[9]))
    return activity_node
    
